<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Members_Entity extends CI_Model
{
    private $tablename = 'members';
    private $id = null;
    private $email = null;
    private $pass = null;
    private $fullname = null;
    private $company = null;
    private $gender = null;
    private $image = null;
    private $date_of_birth = null;
    private $address = null;
    private $phone = null;
    private $created = null;
    private $status = null;
    private $code_forgot = null;
    private $date_forgot = null;
    private $acti_forgot = null;
    private $acticode = null;
    private $sid = null;
    
    public function setId($Id)
    {
        $this->id = $Id;
    }

    public function setEmail($Email)
    {
        $this->email = $Email;
    }

    public function setPass($Pass)
    {
        $this->pass = $Pass;
    }

    public function setFullname($Fullname)
    {
        $this->fullname = $Fullname;
    }

    public function setCompany($Company)
    {
        $this->company = $Company;
    }

    public function setGender($Gender)
    {
        $this->gender = $Gender;
    }

    public function setImage($Image)
    {
        $this->image = $Image;
    }

    public function setDate_of_birth($Date_of_birth)
    {
        $this->date_of_birth = $Date_of_birth;
    }

    public function setAddress($Address)
    {
        $this->address = $Address;
    }

    public function setPhone($Phone)
    {
        $this->phone = $Phone;
    }

    public function setCreated($Created)
    {
        $this->created = $Created;
    }

    public function setStatus($Status)
    {
        $this->status = $Status;
    }

    public function setCode_forgot($Code_forgot)
    {
        $this->code_forgot = $Code_forgot;
    }

    public function setDate_forgot($Date_forgot)
    {
        $this->date_forgot = $Date_forgot;
    }

    public function setActi_forgot($Acti_forgot)
    {
        $this->acti_forgot = $Acti_forgot;
    }

    public function setActicode($Acticode)
    {
        $this->acticode = $Acticode;
    }

    public function setSid($Sid)
    {
        $this->sid = $Sid;
    }

    public function checkExistFK_forum_topics($id)
    {
        $query = $this->db->get_where('forum_topics', array('member_id' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function checkExistFK_forum_comments($id)
    {
        $query = $this->db->get_where('forum_comments', array('member_id' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val == null || $val == '') continue;
            $params[$key] = $val;
        }
                
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        // check exist foreign key
        if ($this->checkExistFK_forum_topics($id)) {
            return array('-1', 'members'.' '.t('is exist in').' '.'forum_topics');
        }
        // check exist foreign key
        if ($this->checkExistFK_forum_comments($id)) {
            return array('-1', 'members'.' '.t('is exist in').' '.'forum_comments');
        }
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getMembersById($id)
    {
        return $this->db->get_where($this->tablename, array('id' => $id));
    }
    
    public function getAllList()
    {
        return $this->db->get($this->tablename);
    }
    
    public function TotalMembers()
    {
        return $this->db->count_all_results('Members'); 
    } 
}       
        